<!DOCTYPE HTML>
<html style="background: #f3f3f3">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>法援宝</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css" />
    <link rel="stylesheet" type="text/css" href="../css/style.css" />
    <link rel="stylesheet" type="text/css" href="../css/swiper.min.css"/>
    <link rel="stylesheet" type="text/css" href="../css/lawyer.css" />
    <script type="text/javascript" src="../script/api.js"></script>
    <script type="text/javascript" src="../script/jquery-1.9.1.min.js"></script>
    <script type="text/javascript" src="../script/swiper.min.js"></script>
    <script type="text/javascript" src="../script/mobile.js"></script>
    <script type="text/javascript" src="../script/lawyer.js"></script>
</head>
<body>
<div id="secHeader">
  <header class="header">
      <a href="javascript:void(0);" onclick="api.closeWin()" class="back_btn"><img src="../image/head-fanhui.png" alt=""></a>
      <h2>发布合同</h2>
      <a href="javascript:;" class="tel_btn rel_btn" onclick="tijiao()">发布</a>
  </header>
</div>

<section class="release_contract">
    <div class="release_list">
        <ul>
            <li>
                <div class="release_item">
                    <p>合同类型</p>
                    <span class="kinds_name"></span>
                </div>
            </li>
            <li>
                <div class="release_item">
                    <p>合同名称</p>
                    <input type="text" placeholder="请输入合同名称" id='name'>
                </div>
            </li>
            <li>
                <div class="release_item">
                    <p>合同价格</p>
                    <input type="text" placeholder="请输入合同价格" id='money'>
                </div>
            </li>
            <li>
                <div class="release_item">
                    <p>合同内容</p>
                </div>
                <textarea name="" id="content" cols="30" rows="10" placeholder="请输入合同内容" style="background: #f3f3f3;box-sizing: border-box;padding: 5px 5px;"></textarea>
            </li>
            <li>
                <div class="release_item">
                    <p>请上传合同图片</p>
                </div>
                <div class="file_list">
                    <div id='imgcon'>


                    </div>

                    <!-- <a href="javascript:void(0);" class="open_camera"></a> -->
                    <div class="open_file" onclick="showAction()"></div>
                    <input type="hidden" name="icon" id='iconid' value="">
                </div>

            </li>
            <li>
                <div class="release_item">
                    <p>请上传合同文件</p>
                </div>
                <div class="file_list">
                    <div id='filecon'>
                        <!--<div class="open_file" style="background: none; word-wrap:break-word;">111111111111111111.txt</div>-->

                    </div>

                    <!-- <a href="javascript:void(0);" class="open_camera"></a> -->
                    <div class="open_file" onclick="showFile()"></div>
                    <input type="hidden" name="file" id='file_id' value="">
                </div>

            </li>
        </ul>
    </div>
</section>
<section class="fixed_select">
    <div class="select_content">
        <h6>请选择合同类型</h6>
        <ul id='cate'>

        </ul>
    </div>
</section>

</body>

<script>
    var type=0;
    var file=0;
    var icon='';
    apiready = function () {
      $api.fixStatusBar( $api.dom('#secHeader') );
      api.setStatusBarStyle({
          style: 'light'
      });
        

        cate();
    };

    function tijiao(){
        if(!loginCheck()){
            return;
        }
        icon=$("input[name=icon]").val();
        if(type==0){
            alert('请选择合同类型');return;
        }else if(icon==0){
            alert('请上传合同图片');return;
        }

        var file = $("input[name=file]").val();

        if(file==0 || file == null || file == ''){
            alert('请上传合同文件');return;
        }
        var money=$('#money').val();

        var name=$('#name').val();
        var content=$('#content').val();
        if(name==''){
            alert('请输入合同名称');return;
        }else  if(content==''){
            alert('请输入合同内容');return;
        }
        if(money==''){
            alert('请输入合同价格');return;
        }else if(!/^\d+$/.test(money)){
            alert('价格必须是数字');return;
        }

        p= {token:token,model:'hetonglawyer',type:type,name:name,money:money,icon:icon,file:file};
         //alert(JSON.stringify(p));
        api.ajax({
            url: webSiteUrl+'/app/user/apply',
            method: 'post',
            data: {
                values: p
            }
        }, function(ret, err) {
            if(ret.code==200){
                alert(ret.msg);
                aaplay('personal_myContract');
            }else{
                alert(ret.msg);
            }
        });

    }






    function showAction() {
    var len=$('#imgcon').children().length;
    if(len==6){
        alert("最多上传6张图片");return;
    }
    api.actionSheet({
         title: '上传图片',
         cancelTitle: '取消',
         buttons: ['拍照', '从手机相册选择']
    }, function(ret, err) {
         if (ret) {
             getPicture(ret.buttonIndex);
         }
    });
}


function showFile() {

            api.actionSheet({
                 title: '请选择上传方式',
                 cancelTitle: '取消',
                 buttons: ['文件管理器']
            }, function(ret, err) {
                 if (ret) {
                     gerFile();
                 }
            });
        }

        function gerFile(){
          var fileBrowser = api.require('fileBrowser');

          fileBrowser.open(function(ret) {
              if (ret) {
                //fileBrowser.skin({skin:1,});//设置文件浏览器皮肤 0 原始皮肤; 1 白色皮肤; 2 灰色皮肤
                fileBrowser.close();//点击文件关闭文件浏览器
                var fileData = ret.url;
                //$("#message").html((JSON.stringify(fileData)))
                api.ajax({
                    url: webSiteUrl+'/app/public/upload_fileht',
                    method: 'post',
                    data: {
                        files: {
                            'file': fileData,
                        }
                    }
                }, function(result, err) {
                    console.log(JSON.stringify(result))
                   // alert(JSON.stringify(ret));
                    if (result.code == 200) {
                        //alert(1)
                        //alert(JSON.stringify(ret));
                        api.hideProgress();
                        addpfile(result.video_id,result.name);
                        //alert(JSON.stringify(result.video_id));
                    } else {
                        api.hideProgress();
                        api.toast({
                            msg: '上传失败！',
                            location: 'middle'
                        });
                    }

                });
              }
          });
        }
        function addpfile(id,name){

            $("#file_id").val(id);
            $('#filecon').html('<div class="open_file" style="background: none;overflow: hidden;word-wrap:break-word;">'+name+'</div>');
        }




function getPicture(sourceType) {
        if (sourceType == 1) { // 拍照
            //获取一张图片
            api.getPicture({
                sourceType: 'camera',
                encodingType: 'png',
                mediaValue: 'pic',
                allowEdit: false,
                quality: 90,
                saveToPhotoAlbum: true
            }, function(ret, err) {
                if(ret.data==''){return;}
                if (ret) {

                    var imgSrc = ret.data;
                    if (imgSrc != "") {

                        //$api.attr(ele, 'src', imgSrc);
                    }
                    if (ret.hasOwnProperty("list")) {

                        //var localAvaterPath = ret.data;
                        //$api.attr(ele, 'src', ret.list[0].path);
                        api.showProgress({
                            title: '正在上传...',
                            modal: true
                        });
                    }
                    var ele = $api.dom('#icon');
                    api.ajax({
                        url: webSiteUrl+'/app/public/uploads',
                        method: 'post',
                        data: {
                            files: {
                                'file': ret.data,
                            }
                        }
                    }, function(ret, err) {
                        if (ret) {
                            if (ret.code == 200) {
                                //alert(JSON.stringify(ret));
                                api.hideProgress();



                                addpic(ret.id,ret.pic);



                            } else {
                                api.hideProgress();
                                api.toast({
                                    msg: ret.msg,
                                    location: 'middle'
                                });
                            }
                        } else {
                            api.hideProgress();
                            api.toast({
                                msg: err.msg,
                                location: 'middle'
                            });
                        }
                    });




                }
            });
        } else if (sourceType == 2) { // 从相机中选择
            //UIMediaScanner 是一个多媒体扫描器，可扫描系统的图片、视频等多媒体资源
            var obj = api.require('UIMediaScanner');
            obj.open({
                //返回的资源种类,picture（图片）,video（视频）,all（图片和视频）
                type: 'picture',
                //（可选项）图片显示的列数，须大于1
                column: 4,
                max: 1,
                //（可选项）图片排序方式,asc（旧->新）,desc（新->旧）
                sort: {
                    key: 'time',
                    order: 'desc'
                },
                //（可选项）模块各部分的文字内容
                texts: {
                    stateText: '已选择*项',
                    cancelText: '取消',
                    finishText: '完成'
                },
                styles: {
                    bg: '#fff',
                    mark: {
                        icon: '',
                        position: 'bottom_right',
                        size: 20
                    },
                    nav: {
                        bg: '#eee',
                        stateColor: '#000',
                        stateSize: 18,
                        cancleBg: 'rgba(0,0,0,0)',
                        cancelColor: '#000',
                        cancelSize: 18,
                        finishBg: 'rgba(0,0,0,0)',
                        finishColor: '#000',
                        finishSize: 18
                    }
                }
            }, function(ret) {
                if(ret.eventType=='cancel'){return;}
                if (ret) {
                    if (ret.hasOwnProperty("list")) {

                        //var localAvaterPath = ret.list[0].path;
                        //$api.attr(ele, 'src', ret.list[0].path);
                        api.showProgress({
                            title: '正在上传...',
                            modal: true
                        });
                    }
                    var ele = $api.dom('#icon');
                    api.ajax({
                        url: webSiteUrl+'/app/public/uploads',
                        method: 'post',
                        data: {
                            files: {
                                'file': ret.list[0].path,
                            }
                        }
                    }, function(ret, err) {
                        if (ret) {
                            if (ret.code == 200) {
                                api.hideProgress();
                                //alert(JSON.stringify(ret));


                                addpic(ret.id,ret.pic);

                            } else {
                                api.hideProgress();
                                api.toast({
                                    msg: ret.msg,
                                    location: 'middle'
                                });
                            }
                        } else {
                            api.hideProgress();
                            api.toast({
                                msg: err.msg,
                                location: 'middle'
                            });
                        }
                    });





                }
            });
        }
    }
    function addpic(id,imgpath){

        var ids=$("#iconid").val();


        if(ids != ''){
            ids+=','+id;
        }else{
            ids=id;
        }
        $("#iconid").val(ids);

        var a='<img src="'+webSiteUrl+imgpath+'" style="display:inline-block"/>';

        $("#imgcon").append(a);

    }

    function upload(){
        var objFile = document.getElementById("fileId");
        if(objFile.value == "") {
            alert("不能空")
        }
        alert(objFile.files[0].size); // 文件字节数
        alert(JSON.stringify(objFile.files[0]))
        var files = $('#fileId').prop('files');//获取到文件列表
        if(files.length == 0){
            alert('请选择文件');
        }
        if (window.FileReader) {    
            var reader = new FileReader();    
            reader.readAsDataURL(objFile.files[0]);    
            //监听文件读取结束后事件    
          reader.onloadend = function (e) {
    alert(e.target.result)
              //e.target.result就是最后的路径地址
            };    
       } return;

        alert(JSON.stringify(objFile.files[0]))
        api.ajax({
            url: webSiteUrl+'/app/public/uploads',
            method: 'post',
            data: {
                files: {
                    'file': files
                }
            }
        }, function(ret, err) {
            if (ret) {
                if (ret.code == 200) {
                    api.hideProgress();
                    alert(JSON.stringify(ret));


                    addpic(ret.id,ret.pic);

                } else {
                    api.hideProgress();
                    alert(ret.error);
                    api.toast({
                        msg: ret.msg,
                        location: 'middle'
                    });
                }
            } else {
                api.hideProgress();
                api.toast({
                    msg: err.msg,
                    location: 'middle'
                });
            }
        });
    }
    function cate(){
        var p={type:114};
        api.ajax({
            url: webSiteUrl+'/app/public/cate',
            method: 'post',
            data: {
                values:p
            }
        }, function(ret, err) {
            if(ret.code==200){

                var tj=ret.data;
                html = "";
                for(var i=0;i<tj.length;i++){
                    html+='<li  data="'+tj[i].id+'" style="cursor:pointer">'+tj[i].title+'</li>';
                }
                $('#cate').html(html);
            }
        });

    }

    //发布合同 类型选择
    $(".kinds_name").click(function () {
        $(".fixed_select").fadeToggle();
        $(".fixed_select .select_content").animate({
            'top': '0',
            'opacity': '1'
        },400);
    });
    $(document).on('click',".select_content ul li",function(){
        type=$(this).attr('data');
        $(".kinds_name").text($(this).html());
        $(".fixed_select").fadeToggle();
        $(".fixed_select .select_content").animate({
            'top': '-4rem',
            'opacity': '0'
        },400);
    })
</script>
</html>
